package com.cc.controller

import com.cc.common.model.Query
import com.cc.common.model.Results
import com.cc.model.Category
import com.cc.model.dto.CategoryDto
import com.cc.service.CategoryService
import com.mybatisflex.core.query.QueryWrapper
import org.springframework.web.bind.annotation.*


/**
 * 分类管理API
 *
 * @author aa
 * @version 1.0
 * @since 2024/4/25
 */
@RequestMapping("/dataCollection/category")
@RestController
class CategoryController(private val categoryService: CategoryService) {


    /**
     * 添加分类
     *
     * @param category [category]
     * @return [Result]
     */
    @PostMapping("/add")
    fun create(@RequestBody category: CategoryDto) = Results.Ok(categoryService.create(category))


    /**
     * 通过id删除分类
     *
     * @param id 要删除的数据的id
     * @return [Result]
     */
    @DeleteMapping("/delete")
    fun delete(@RequestParam("id", defaultValue = "") id: String) = Results.Ok(categoryService.removeById(id))


    /**
     * 批量删除分类
     *
     * @param ids 要删除数据的[List]集
     * @return [Result]
     */
    @DeleteMapping("/deleteBatch")
    fun deleteBatch(@RequestBody ids: List<String>) = Results.Ok(categoryService.removeByIds(ids))


    /**
     * 更新分类
     *
     * @param category [category]
     * @return [Result]
     */
    @PutMapping("/update")
    fun update(@RequestBody category: Category) = Results.Ok(categoryService.updateById(category))

    /**
     * 根据条件查询分类列表
     * @param query
     * @return
     */
    @PostMapping("/findCategoryListByPage")
    fun findCategoryListByPage(@RequestBody query: Query<CategoryDto?>) = Results.Ok(categoryService.findCategoryListByPage(query))


    /**
     * 查询分类明细
     *
     * @param id 根据id查询
     * @return [Result]
     */
    @GetMapping("/getCategoryDetail")
    fun getCategoryDetail(@RequestParam("id", defaultValue = "") id: String) = Results.Ok(categoryService.getById(id))

    /**
     * 查询分类列表
     *
     * @return [Result]
     */
    @GetMapping("/findCategoryList")
    fun findCategoryList() = Results.Ok(categoryService.list())


    /**
     * 验证分类名是否存在
     *
     * @param categoryName 分类名
     * @return 只能返回true或false
     */
    @PostMapping("/categoryExist")
    fun categoryExist(@RequestParam("categoryName", defaultValue = "") categoryName: String) =
        Results.Ok(categoryService.exists(QueryWrapper.create().eq(Category::categoryName, categoryName)))


    /**
     * 根据条件查询转换列表
     * TYPE=1为查询转换，2为查询作业,默认查询全部
     * @param [cid] CID公司
     * @param [name] 名字
     * @param [pageNum] 页码
     * @param [pageSize] 页面大小
     * @param [type] 类型
     */
    @GetMapping("/findListByPageSimple")
    fun findListByPageSimple(
        @RequestParam("cid") cid: String,
        @RequestParam(name = "name", required = false, defaultValue = "") name: String,
        @RequestParam(name = "pageNum", required = false, defaultValue = "1") pageNum: Int,
        @RequestParam(name = "pageSize", required = false, defaultValue = "10") pageSize: Int,
        @RequestParam(name = "type") type: Int?,
    ) = Results.Ok(categoryService.findListByPageSimple(cid, name, pageNum, pageSize, type))

}
